package com.gitee.wsl.func.procedure.sample

import com.gitee.wsl.func.predicate.Predicate
import com.gitee.wsl.func.procedure.Procedure
import com.gitee.wsl.func.Function

class CollectIfProcedure<T, V>(
    val collection: MutableList<V>,
    private val function: Function<in T, out V>,
    private val predicate: Predicate<in T>,
) : Procedure<T> {

    constructor(
        taskSize: Int,
        function: Function<in T, out V>,
        predicate: Predicate<in T>,
    ) : this(ArrayList(taskSize), function, predicate)

    override fun value(each: T) {
        if (this.predicate.accept(each)) {
            this.collection.add(this.function(each))
        }
    }

}
